#include "ThreePlayer.h"


extern IDirectInputDevice8		*mJoystick[2]; // This will be changed to an area later...
extern DIJOYSTATE2				js;
//extern 	int prevJSButtons[12];
//extern 	int prevJSButtons2[12];
//extern 	int prevJS;
extern 	ID3DXSprite* mSprite;

ThreePlayer* ThreePlayer::init = 0;

ThreePlayer::ThreePlayer()
{
	HR(D3DXCreateTextureFromFile(gd3dDevice, "BallSmackersThreePlayerTest.png", &ThreePlayerMenuTex));
	ThreePlayerMenuCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);
	HR(D3DXCreateTextureFromFile(gd3dDevice, "checkmark.png", &CheckMarkTex));
	CheckMarkCenter = D3DXVECTOR3(32.0f, 32.0f, 0.0f);
	choice = '0';
}


void ThreePlayer::Enter()
{
//	cout << "Creating stuff for Two Player\n";
//	
}

void ThreePlayer::Execute(D3DApp* g)
{

			
	static float cursorDelay = 0.0f;
	static float timeAccum = 0.0f;
	static int prevJS = 0;
	mJoystick[0]->GetDeviceState(sizeof(DIJOYSTATE2), &js);
		
	if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 0) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "1st", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 9000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "2nd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 18000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "3rd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 27000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "4th", "Error", 0);
	}
	else if((js.rgbButtons[0] & 0x80) && (prevJSButtons[0] ^ js.rgbButtons[0]))
	{
		choice = '1';
	}

	else if(js.rgbButtons[1] & 0x80 && (prevJSButtons[1] ^ js.rgbButtons[1]))
	{
		choice = '2';
	}

	else if(js.rgbButtons[2] & 0x80 && (prevJSButtons[2] ^ js.rgbButtons[2]))
	{
		choice = '3';
	}

	else if(js.rgbButtons[3] & 0x80 && (prevJSButtons[3] ^ js.rgbButtons[3]))
	{
		gd3dApp->setState(State_Menu::Instance());
	}

	if(!(js.rgdwPOV[0] == 4500 || js.rgdwPOV[0] == 13500 || js.rgdwPOV[0] == 22500 || js.rgdwPOV[0] == 31500))
			prevJS = js.rgdwPOV[0];

	for(int i = 0; i < 12; i++)
	{
		prevJSButtons[i] = js.rgbButtons[i];
	}
	Draw();

}

void ThreePlayer::Exit()
{
//	cout << "Exiting State_ThreePlayer\n";
}

void ThreePlayer::Draw()
{
	////HR(D3DXCreateTextureFromFile(gd3dDevice, "BallSmackersThreePlayerTest.png", &ThreePlayerMenuTex));
	////ThreePlayerMenuCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);

    // Now we can clear just view-port's portion of the buffer to red...
   HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

    HR(gd3dDevice->BeginScene());
    
    //draw stuff here
	HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, -260.0f, -140.0f, 0.0f);
	D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	//draw the menu
	HR(mSprite->Draw(ThreePlayerMenuTex, 0, &ThreePlayerMenuCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());

	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	if (choice == '1')
	{
		D3DXMATRIX texScaling;
		D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

		D3DXMATRIX T, S;
		D3DXMatrixTranslation(&T, -620.0f, 300.0f, 0.0f);
		D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
		HR(mSprite->SetTransform(&(S*T)));

		//draw the menu
		HR(mSprite->Draw(CheckMarkTex, 0, &CheckMarkCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
		HR(mSprite->Flush());

		//restore the texture coordinate scaling transform. mirror texture 
		//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
		D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	}
	if (choice == '2')
	{
		D3DXMATRIX texScaling;
		D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

		D3DXMATRIX T, S;
		D3DXMatrixTranslation(&T, -620.0f, 220.0f, 0.0f);
		D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
		HR(mSprite->SetTransform(&(S*T)));

		//draw the menu
		HR(mSprite->Draw(CheckMarkTex, 0, &CheckMarkCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
		HR(mSprite->Flush());

		//restore the texture coordinate scaling transform. mirror texture 
		//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
		D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	}
	if (choice == '3')
	{
		D3DXMATRIX texScaling;
		D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

		D3DXMATRIX T, S;
		D3DXMatrixTranslation(&T, -620.0f, 140.0f, 0.0f);
		D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
		HR(mSprite->SetTransform(&(S*T)));

		//draw the menu
		HR(mSprite->Draw(CheckMarkTex, 0, &CheckMarkCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
		HR(mSprite->Flush());

		//restore the texture coordinate scaling transform. mirror texture 
		//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
		D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
		HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	}

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	HR(gd3dDevice->EndScene());
	


	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));

}

ThreePlayer* ThreePlayer::Instance()
{
	if(init == 0)
	{
		init = new ThreePlayer();
	}
	return init;
}